package china.accountinghelper.business;

import java.util.List;

import com.xdroid.base.BusinessBase;

import china.wc.tallyhelper.database.SQLiteDALModelExpenseDetail;
import china.wc.tallyhelper.model.ModelExpense;
import china.wc.tallyhelper.model.ModelExpenseDetail;
import china.wc.tallyhelper.model.ModelPerson;

/**
 * 本类是所有业务类的父类，封装和ModelExpenseDetail实体类相关的业务逻辑。
 * 本类的作用是将DAL层的方法进一步封装，并将其返回结果进行必要的处理，方便Activity、Adapter进行调用。
 * 
 * @author 王超
 * 
 */
public class BusinessExpenseDetail extends BusinessBase<ModelExpenseDetail>
{
    private BusinessPerson mBusinessPerson = new BusinessPerson();

    public BusinessExpenseDetail()
    {
        super(new SQLiteDALModelExpenseDetail(ModelExpenseDetail.class));
    }
    
    /**
     * 传入消费明细，返回消费人的姓名
     * @param pExpenseDetail
     * @return
     */
    public String getConsumerName(ModelExpenseDetail pExpenseDetail)
    {
        ModelPerson _Consumer = mBusinessPerson.select(pExpenseDetail.getConsumerID());
        
        return _Consumer.getName();
    }

    /**
     * 传入ModelExpense，返回该Expense下的消费明细。
     */
    public List<ModelExpenseDetail> select(ModelExpense pExpense)
    {
        String _ColumnName = getSQLiteDAL().getFieldColumnMap().get("mExpenseID");
        List<ModelExpenseDetail> _ModelList = getSQLiteDAL().select(_ColumnName + "=" + pExpense.getID(), null, null, null, null);
        
        return _ModelList;
    }
    
    /**
     * 传入ModelExpense，返回该Expense下的消费总额。
     * 单位为元。
     */
    public double count(ModelExpense pExpense)
    {
        double _SumMoney = 0;
            List<ModelExpenseDetail> _ExpenseDetails = select(pExpense);
            for (ModelExpenseDetail _TemExpenseDetail : _ExpenseDetails) {
                _SumMoney = _SumMoney +_TemExpenseDetail.getMoney2();
            }
        return _SumMoney;
    }
    
    public boolean insertModel(List<ModelExpenseDetail> pExpenseDetails)
    {
        boolean _Result = true;
        
        getSQLiteDAL().beginTransaction();
        try {
            for (ModelExpenseDetail _ExpenseDetail : pExpenseDetails) {
                boolean _InsertIsOk = insertModel(_ExpenseDetail);
                if (!_InsertIsOk)      _Result = false;
            }
            if (_Result == true)    getSQLiteDAL().setTransactionSuccessful();
        } finally {
            getSQLiteDAL().endTransaction();
        }
        
        return _Result;
    }
    
    public boolean updateModel(List<ModelExpenseDetail> pExpenseDetails)
    {
        boolean _Result = true;
        
        getSQLiteDAL().beginTransaction();
        try {
            for (ModelExpenseDetail _ExpenseDetail : pExpenseDetails) {
                boolean _UpdateIsOk = updateModel(_ExpenseDetail);
                if (!_UpdateIsOk)      _Result = false;
            }
            if (_Result == true)    getSQLiteDAL().setTransactionSuccessful();
        } finally {
            getSQLiteDAL().endTransaction();
        }
        
        return _Result;
    }
    
    public boolean deleteModel(List<ModelExpenseDetail> pExpenseDetails)
    {
        boolean _Result = true;
        
        getSQLiteDAL().beginTransaction();
        try {
            for (ModelExpenseDetail _ExpenseDetail : pExpenseDetails) {
                boolean _DeleteIsOk = deleteModel(_ExpenseDetail);
                if (!_DeleteIsOk)      _Result = false;
            }
            if (_Result == true)    getSQLiteDAL().setTransactionSuccessful();
        } finally {
            getSQLiteDAL().endTransaction();
        }
        
        return _Result;
    }

}
